package org.ucas.faker.web.service.Visualization;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.ucas.faker.web.Config.VisualConfig;
import org.ucas.faker.web.fetcher.FetchReqBuilder;
import org.ucas.faker.web.service.Visualization.json.JsonUtil;

@Service
@Transactional
public class CallbackService {

    public void handleGeneralCallback(JSONObject graphJson, JSONObject callbackJson, FetchReqBuilder requestBuilder){
        String basisKey = JsonUtil.getValue(graphJson, "basis.key");
        int dataIndex = JsonUtil.getValue(callbackJson, "dataIndex");

        if (JsonUtil.containsKey(graphJson, "basis.range")){
            String basisRangeType = JsonUtil.getValue(graphJson, "basis.range.type");
            Object basisRangeValue = JsonUtil.getValue(graphJson, "basis.range.value");
            Object basisValue = null;
            switch (basisRangeType){
                case VisualConfig.Json.Value.RANGE_TYPE_AUTO:
                    basisValue = JsonUtil.getValue(callbackJson, "name");
                    requestBuilder.eq(basisKey, basisValue);
                    break;
                case VisualConfig.Json.Value.RANGE_TYPE_INTERVAL:
                    break;
                case VisualConfig.Json.Value.RANGE_TYPE_HYPERSET:
                case VisualConfig.Json.Value.RANGE_TYPE_SET:
                    basisValue = ((JSONArray)basisRangeValue).get(dataIndex);
                    if (basisValue instanceof JSONArray){
                        if (((JSONArray) basisValue).size()>0) {
                            requestBuilder.in(basisKey, JsonUtil.jsonArrayToList((JSONArray) basisValue));
                        }
                    } else {
                        requestBuilder.eq(basisKey, basisValue);
                    }
                    break;
                case VisualConfig.Json.Value.RANGE_TYPE_TOP:
                    break;
                default:
                    break;
            }
        } else {
            //TODO
        }

        if (JsonUtil.containsKey(graphJson, "branch")){
            String branchKey = JsonUtil.getValue(graphJson, "branch.key");
            int seriesIndex = JsonUtil.getValue(callbackJson, "seriesIndex");

            if (JsonUtil.containsKey(graphJson, "branch.range")){
                String branchRangeType = JsonUtil.getValue(graphJson, "branch.range.type");
                Object branchRangeValue = JsonUtil.getValue(graphJson, "branch.range.value");
                Object branchValue = null;
                switch (branchRangeType){
                    case VisualConfig.Json.Value.RANGE_TYPE_AUTO:
                        branchValue = JsonUtil.getValue(callbackJson, "seriesName");
                        requestBuilder.eq(branchKey, branchValue);
                        break;
                    case VisualConfig.Json.Value.RANGE_TYPE_HYPERSET:
                    case VisualConfig.Json.Value.RANGE_TYPE_SET:
                        branchValue = ((JSONArray)branchRangeValue).get(seriesIndex);
                        if (branchValue instanceof JSONArray){
                            if (((JSONArray) branchValue).size()>0) {
                                requestBuilder.in(branchKey, JsonUtil.jsonArrayToList((JSONArray) branchValue));
                            }
                        } else {
                            requestBuilder.eq(branchKey, branchValue);
                        }
                        break;
                    default:
                        break;
                }
            } else {
                //TODO
            }
        }
    }
}
